<?php
   //
   //
   function GetTimers(&$stamp, &$enqueueTime, &$clientEnqueueTime)
   {
      list($l1, $l2) = explode(" ", microtime());
      $b1 = bcmul($l2, '1000.0', 3);
      $b2 = bcmul($l1, '1000.0', 3);
      $stamp = bcadd($b1, $b2, 0);

      $s1 = strval(rand(0, 1000));
      $b3 = bcsub($stamp, $s1, 3);
      $enqueueTime = (float)bcdiv($b3, '1000.0', 3);
      $clientEnqueueTime = (float)bcdiv($b3, '1000.0', 0);
   }

      if (!isset($argv[2])) exit;
      $data = base64_decode($argv[2]);
      $data = json_decode($data,true);


      date_default_timezone_set('Europe/London');

      define('AMFPHP_BASE', 'amfphp/core/');
      require_once(AMFPHP_BASE . "shared/util/CharsetHandler.php");
      require_once(AMFPHP_BASE . "shared/util/CompatPhp5.php");
      require_once(AMFPHP_BASE . "shared/util/MessageBody.php");
      require_once(AMFPHP_BASE . "shared/app/Constants.php");
      require_once(AMFPHP_BASE . "shared/app/Globals.php");
      require_once(AMFPHP_BASE . "amf/io/AMFDeserializer.php");
      require_once(AMFPHP_BASE . "amf/io/AMFSerializer.php");
      require_once(AMFPHP_BASE . "amf/util/AMFObject.php");

      $amfbin_file = 'base.amf';
      try
      {
         $f = fopen($amfbin_file, 'r');
         $size = filesize($amfbin_file);
         $amf = new AMFObject(fread($f, $size));
         fclose($f);
      }catch(Exception $e)
      {
         $this->SendMsg($e->getMessage());
         exit;
      }

      $deserializer = new AMFDeserializer($amf->rawData);
      $deserializer->deserialize($amf);

      $bod = new MessageBody();
      $bod = $amf->_bodys[0];

      //changing the body to actual data provided by the Bot program
      $bod->_value[0]['zySnid']        = $data['zySnid'];
      $bod->_value[0]['zySig']         = $data['zySig'];
      $bod->_value[0]['zyAuthHash']    = $data['zyAuthHash'];
      $bod->_value[0]['flashRevision'] = (int)$data['flashRevision'];
      //
      GetTimers($stamp, $enqueueTime, $clientEnqueueTime);
      $bod->_value[1][0]['stamp'] = floatval($stamp);
      $bod->_value[1][0]['enqueueTime'] = floatval($enqueueTime);
      $bod->_value[1][0]['params'][0]  = $data['firstname'];
      $bod->_value[1][0]['params'][1]  = $data['session'];


      $amf = new AMFObject();
      $amf->addBody($bod);
      $serializer = new AMFSerializer();
      $rawdata = $serializer->serialize($amf);

      $tmp = 'tmp_dir\_amf'.$argv[1].'.bin';
      if(file_exists($tmp)) unlink($tmp); //delete the file if exists
      try
      {
         $f = fopen($tmp, 'x');
         fwrite($f,$rawdata);
         fclose($f);
      }catch(Exception $e)
      {
         $this->SendMsg($e->getMessage());
         exit;
      }
?>